/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servidor;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import servgui.*;

/**
 *
 * @author Kaotar
 */
public class ServidorMultThread implements Runnable {

    private ServerSocket serv;
    private boolean listen;
    private ArrayList<Servidor> users = new ArrayList();
    private ArrayList<Channel> channels = new ArrayList();
    private ServGuiApp gui;

    public ArrayList<Channel> getChannels() {
        return channels;
    }

    public ArrayList<Servidor> getUsers() {
        return users;
    }

    public void run() {
        try {
            while (listen) {
                Thread t = new Thread(new Servidor(serv.accept(), this));
                t.start();
            }
        } catch (IOException e) {
            printMessage(e.getMessage());
        }
    }

    public ServidorMultThread(ServGuiApp gui) {
        try {
            //Cria o ServerSocket na porta 7000 se estiver disponível  
            serv = new ServerSocket(7000);
            this.gui = gui;
            //Aguarda uma conexão na porta especificada e cria retorna o socket que irá comunicar com o cliente  
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public void ligar() {
        if (!listen) {
            listen = true;
            Thread t = new Thread(this);
            t.start();
            printMessage("Servidor iniciado com sucesso!");
        }
    }

    public void desligar() {
        if (listen) {
            listen = false;
            printMessage("Servidor desligado!");
        }
    }

    public void printMessage(String str) {
        gui.printMessage(str);
    }

    public boolean isNickAlreadyUsed(String nick) {
        for (Servidor i : users) {
            if (i.getNick().compareToIgnoreCase((nick)) == 0) {
                return true;
            }
        }
        return false;
    }

    public int isConnected(Servidor user) {
        return users.indexOf(user);
    }

    public Servidor getUserByNick(String nick) {
        for (Servidor i : users) {
            if (i.getNick().compareToIgnoreCase((nick)) == 0) {
                return i;
            }
        }
        return null;
    }

    public int connect(Servidor user) {
        if (isNickAlreadyUsed(user.getNick()) == false) {
            users.add(user);
            return 0;
        } else {
            return -1;
        }
    }

    public int disconnectServ(String name) {
        name = "\"" + name + "\"";
        Servidor user = getUserByNick(name);
        if (user != null) {
            disconnect(user);
            return 0;
        } else {
            return -1;
        }
    }

    public void disconnect(Servidor user) {
        int index = users.indexOf(user);
        if (index != -1) {
            users.remove(index);
        }
        for (Channel i : channels) {
            i.leaveChannel(user);
        }
    }

    public Channel searchChannel(String nameChannel) {
        if (nameChannel.charAt(1) != '#') {
            return null;
        }
        for (Channel i : channels) {
            if (i.getName().compareToIgnoreCase(nameChannel) == 0) {
                return i;
            }
        }
        return null;
    }

    public int joinChannel(String nameChannel, Servidor user) {
        Channel chan = searchChannel(nameChannel);
        if (chan != null) {
            return chan.joinChannel(user);         //-1 Ja esta na sala
        }
        return -2; //-2 Sala nao existe
    }

    public int createChannel(String name, boolean servIs) { //TODO
        name = "\"" + name + "\"";
        Channel chan = searchChannel(name);
        if (chan == null) {
            chan = new Channel(name, servIs, this);
            channels.add(chan);
            return 0;
        } else {
            return -1;
        }
    }

    public int deleteChannelByStr(String name) {
        name = "\"" + name + "\"";
        Channel chan = searchChannel(name);
        if (chan != null) {
            deleteChannel(chan);
            return 0;
        } else {
            return -1;
        }
    }

    public void deleteChannel(Channel chan) {
        channels.remove(chan);
    }
}
